package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCatDTO;
import com.sky.entity.DishEntity;
import com.sky.entity.SetmealEntity;
import com.sky.entity.ShoppingCatEntity;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCatMapping;
import com.sky.service.ShoppingCatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCatServiceImpl implements ShoppingCatService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private ShoppingCatMapping shoppingCatMapping;

    @Override
    @Transactional
    public void add(ShoppingCatDTO catDTO) {
        ShoppingCatEntity entity = new ShoppingCatEntity();
        BeanUtils.copyProperties(catDTO, entity);

        List<ShoppingCatEntity> list = shoppingCatMapping.list(entity);

        if (list != null && list.size() > 0) {
            // 更新number
            ShoppingCatEntity shoppingCatEntity = list.get(0);
            shoppingCatMapping.updateWithNumberById(shoppingCatEntity.getId(), shoppingCatEntity.getNumber() + 1);
        } else {
            // 新增到购物车
            if (catDTO.getDishId() != null) {
                DishEntity dishEntity = dishMapper.getById(Math.toIntExact(catDTO.getDishId()));
                entity.setName(dishEntity.getName());
                entity.setImage(dishEntity.getImage());
                entity.setAmount(Math.toIntExact(dishEntity.getPrice()));
            } else {
                SetmealEntity setmealEntity = setmealMapper.getById(Long.valueOf(catDTO.getSetmealId()));
                entity.setName(setmealEntity.getName());
                entity.setImage(setmealEntity.getImage());
                entity.setAmount(Math.toIntExact(setmealEntity.getPrice()));
            }
            entity.setNumber(1);
            entity.setUserId(BaseContext.getId());
            entity.setCreateTime(LocalDateTime.now());

            shoppingCatMapping.insert(entity);

        }
    }

    @Override
    public List<ShoppingCatEntity> list() {
        ShoppingCatEntity entity = ShoppingCatEntity.builder().id(BaseContext.getId()).build();
        List<ShoppingCatEntity> list = shoppingCatMapping.list(entity);

        return list;
    }

    @Override
    public void clean() {
        shoppingCatMapping.clean(BaseContext.getId());
    }

    @Override
    public void sub(ShoppingCatDTO catDTO) {
        ShoppingCatEntity entity = new ShoppingCatEntity();
        BeanUtils.copyProperties(catDTO, entity);
        entity.setUserId(BaseContext.getId());

        // 首先查询数量是否 > 1 ，如果大于1，则减1
        List<ShoppingCatEntity> list = shoppingCatMapping.list(entity);
        if (list != null && list.size() > 0) {
            ShoppingCatEntity shoppingCatEntity = list.get(0);
            if (shoppingCatEntity.getNumber() > 1) {
                // 则减去1
                shoppingCatMapping.updateWithNumberById(shoppingCatEntity.getId(), shoppingCatEntity.getNumber() - 1);
            } else {
                // 删除
                shoppingCatMapping.sub(entity);
            }
        }
    }
}
